<template>
  <el-dialog v-if="load" title="更新1688链接" v-model="visible" append-to-body :close-on-click-modal="false" :z-index="2000" width="500px" @close="handleClose">
    <el-form label-position="top">
      <el-form-item label="商品链接">
        <el-input v-model="form.goods_link" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">匹 配</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from './api'
import XEUtils from 'xe-utils'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const item = ref({})

const emit = defineEmits(['close', 'change'])

// 打开弹窗
const open = async (row) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = XEUtils.clone(row, true)
  item.value = row
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  const { goods_link, sku, p_id } = form.value
  loadings.submit = true
  api.match({ goods_link,sku, p_id }).then(res => {
    Object.assign(item.value, res.data, {goods_link, assoc_sku_info: [], seclected_sku_info: []})
    emit('change')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>